package com.atech.ebayadmin.system.service.impl;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.atech.ebayadmin.common.pager.Pager;
import com.atech.ebayadmin.common.util.DateUtil;
import com.atech.ebayadmin.system.dao.ISystemDAO;
import com.atech.ebayadmin.system.dao.IUserDAO;
import com.atech.ebayadmin.system.model.Role;
import com.atech.ebayadmin.system.model.SystemConfig;
import com.atech.ebayadmin.system.model.User;
import com.atech.ebayadmin.system.service.ISystemService;
import com.atech.ebayadmin.system.vo.UserVO;

public class SystemService implements ISystemService{
	private final static org.apache.commons.logging.Log log = 
		org.apache.commons.logging.LogFactory.getLog(SystemService.class);
    private ISystemDAO systemDAO;
    private IUserDAO userDAO;
	public IUserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}
	/**
	 * @return the systemDAO
	 */
	public ISystemDAO getSystemDAO() {
		return systemDAO;
	}
	/**
	 * @param systemDAO the systemDAO to set
	 */
	public void setSystemDAO(ISystemDAO systemDAO) {
		this.systemDAO = systemDAO;
	}
	public List<Role> listAllRole()throws Exception{
		return systemDAO.listAllRole();
	}
	public SystemConfig getSystemConfig()throws Exception{
		List<SystemConfig> list = systemDAO.listSystemConfig();
		if(list != null && list.size() >0){
			return list.get(0);
		}
		return null;
	}
	public void updatePassword(User user) throws Exception{
		User originUser = userDAO.getUser(user.getUserId());
		System.out.println(user.getPassword());
		System.out.println(originUser.getPassword());
		if( ! user.getPassword().equals(originUser.getPassword())){
			throw new Exception("尊敬的用户，用户原密码不正确！");
		}
		originUser.setPassword(user.getNewPassword());
		userDAO.updateUser(originUser);
	}

	public List<String> listAllNeedFiltUrl()throws Exception{
 		return systemDAO.listAllPermission();
	}
	public boolean isHasPermission(Integer roleId,String url){
		return systemDAO.isHasPermission(roleId, url);
	}
	public UserVO login(String userName,String password) throws Exception{
		UserVO userVO = new UserVO();
    	User user = userDAO.getUser(userName);
    	if(user == null){
    		throw new Exception("尊敬的用户，用户名错误！");
    	}else{
    		if( ! user.getPassword().equals(password)){
    			throw new Exception("尊敬的用户，密码错误！");
    		}
    		userVO.setRoleId(user.getRoleId());
    		userVO.setUserId(user.getUserId());
    		userVO.setUserName(user.getUserName());
    		userVO.setLoginTime(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
    		return userVO;
    	}
	}
    public Integer saveUser(User po) throws Exception{
    	po.setPassword(po.getPassword());
    	po.setIsDeleted(0);
    	Integer userId = userDAO.saveUser(po);
		
    	return userId;
    }
    public void updateUser(User po) throws Exception{
    	User originUser = userDAO.getUser(po.getUserId());
    	originUser.setRoleId(po.getRoleId());

    	if (StringUtils.isNotEmpty(po.getPassword()) && null != po.getPassword()) {
    		originUser.setPassword(po.getPassword());
    	} else {
    		throw new Exception("密码不能为空!");
    	}
    	userDAO.updateUser(originUser);//更新原来的用户信息
    }
    public void deleteUser(Integer userId) throws Exception{
    	User originUser = userDAO.getUser(userId);
    	originUser.setIsDeleted(1);//将删除标识设为已删除
    	userDAO.updateUser(originUser);
    }
    public List<User> listUser(String userName, Pager pager) throws Exception{
    	List<User> userList = userDAO.listUser(userName, pager);
    	for(User user : userList){
    		this.setUserPo(user);
    	}
    	return userList;
    }
    public User getUser(Integer userId) throws Exception{
    	User user = userDAO.getUser(userId);
    	if(user.getIsDeleted() == 1 ){
    		throw new Exception("该用户已被删除!");
    	}
    	this.setUserPo(user);
    	return user;
    }
    //重新设置用户类的数据
    private void setUserPo(User user)throws Exception{
    	Role role = systemDAO.getRole(user.getRoleId());
    	user.setRoleName(role.getRoleName());
    }
    
}
	
